package util;

import javafx.scene.control.DateCell;
import javafx.scene.control.DatePicker;
import javafx.util.Callback;

import java.time.LocalDate;

public class SetDatePicker {
    public static void setDatePicker(DatePicker datePicker){
        LocalDate today = LocalDate.now();
        // 创建一个日期单元格工厂并设置日期限制
        Callback<DatePicker, DateCell> dayCellFactory = dp -> new DateCell() {
            @Override
            public void updateItem(LocalDate item, boolean empty) {
                super.updateItem(item, empty);
                if (item.isAfter(today)) {
                    setDisable(true);
                    setStyle("-fx-background-color: #ffc0cb"); // 禁用的日期将是粉色的
                }
            }
        };
        // 设置日期单元格工厂
        datePicker.setDayCellFactory(dayCellFactory);
    }
    public static void setDatePickerBefore(DatePicker datePicker){
        LocalDate today = LocalDate.now();
        // 创建一个日期单元格工厂并设置日期限制
        Callback<DatePicker, DateCell> dayCellFactory = dp -> new DateCell() {
            @Override
            public void updateItem(LocalDate item, boolean empty) {
                super.updateItem(item, empty);
                if (item.isBefore(today)) {
                    setDisable(true);
                    setStyle("-fx-background-color: #ffc0cb"); // 禁用的日期将是粉色的
                }
            }
        };
        // 设置日期单元格工厂
        datePicker.setDayCellFactory(dayCellFactory);
    }
    public static void setDatePickerDefinition(DatePicker datePicker,DatePicker standard){
        // 创建一个日期单元格工厂并设置日期限制
        Callback<DatePicker, DateCell> dayCellFactory = dp -> new DateCell() {
            @Override
            public void updateItem(LocalDate item, boolean empty) {
                super.updateItem(item, empty);
                if (item.isBefore(standard.getValue())) {
                    setDisable(true);
                    setStyle("-fx-background-color: #ffc0cb"); // 禁用的日期将是粉色的
                }
            }
        };
        // 设置日期单元格工厂
        datePicker.setDayCellFactory(dayCellFactory);
    }
}
